package edu.dk.controller;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import edu.dk.model.IPAddress;
import edu.dk.model.Resource;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class ResourceController {


    @RequestMapping(value = "/resources",  method = RequestMethod.GET)
    public ModelAndView resources() {
		ModelAndView mav = new ModelAndView();
		List<String> resources = new LinkedList<String>();
		resources.add("A");
		resources.add("B");
		resources.add("C");
		mav.addObject("resources", resources);
		mav.setViewName("resources");
		return mav;
    }
/*
    @RequestMapping(value = "/news/all",  method = RequestMethod.GET)
    public ModelAndView showAllNews() {
	ModelAndView mav = new ModelAndView();
	MessageSource messageSource = ManagerUtil.getInstance()
	.getMessageSource();
	List<News> newsList = ManagerUtil.getInstance()
	.getNewsManager().getAll();
	if (newsList.isEmpty()) {
	    mav.addObject("message", messageSource
		    .getMessage("message.noneNews",
			    null, Locale.ROOT));
	} else {
	    mav.addObject("message", messageSource
		    .getMessage("message.archiveNews",
			    null, Locale.ROOT));
	}
	mav.addObject("news", newsList);
	mav.setViewName("news");
	return mav;
    }

    @RequestMapping(value = "/news/new",  method = RequestMethod.GET)
    public ModelAndView createNews() {
	ModelAndView mav = new ModelAndView();
	mav.addObject("news", new News());
	mav.setViewName("createNews");
	return mav;
    }

    @RequestMapping(value = "/news/new",  method = RequestMethod.POST)
    public ModelAndView createSubmit(@ModelAttribute("news")
	    News news, BindingResult result) {
	ModelAndView mav = new ModelAndView();
	NewsValidator newsValidator = ManagerUtil.getInstance()
	.getNewsValidator();
	newsValidator.validate(news, result);
	if (result.hasErrors()) {
	    mav.setViewName("createNews");
	    return mav;
	}
	news.setCreationDate(Calendar.getInstance());
	news.setAuthor(ManagerUtil.getInstance().getUserManager()
		.getOnlineUser());
	ManagerUtil.getInstance().getNewsManager().add(news);
	mav.setViewName("redirect:/news");
	return mav;
    }

    @RequestMapping(value = "/news/details/{id}",
	    method = RequestMethod.GET)
    public ModelAndView showDetails(@PathVariable int id) {
	ModelAndView mav = new ModelAndView();
	News news = ManagerUtil.getInstance().getNewsManager().get(id);
	if (news != null) {
	    mav.addObject("news", news);
	} else {
	    mav.setViewName("redirect:/news");
	    return mav;
	}
	mav.setViewName("newsDetails");
	return mav;
    }

    @RequestMapping(value = "/news/{action}/{id}",
	    method = RequestMethod.GET)
    public ModelAndView editOrDeleteNews(@PathVariable
		    String action, @PathVariable int id) {
	ModelAndView mav = new ModelAndView();
	if (action.equals("delete")) {
	    ManagerUtil.getInstance().getNewsManager().delete(id);
	    mav.setViewName("redirect:/news");
	    return mav;
	} else {
	    News news = ManagerUtil.getInstance()
	    .getNewsManager().get(id);
	    if (news != null) {
		mav.addObject("news", news);
		mav.setViewName("createNews");
		return mav;
	    }
	    mav.setViewName("redirect:/news");
	    return mav;
	}
    }

    @RequestMapping(value = "/news/{action}/{id}",
	    method = RequestMethod.POST)
    public String submitEditOrDeleteNews(@ModelAttribute("news")
	    	News news, BindingResult result,
	    @PathVariable String action,
	    @PathVariable int id) {
	NewsValidator newsValidator = ManagerUtil.getInstance()
	.getNewsValidator();
	newsValidator.validate(news, result);
	if (result.hasErrors()) {
	    return "createNews";
	}
	news.setCreationDate(ManagerUtil.getInstance().getNewsManager()
		.get(id).getCreationDate());
	news.setAuthor(ManagerUtil.getInstance().getUserManager()
		.getOnlineUser());
	ManagerUtil.getInstance().getNewsManager().update(news);
	return "redirect:/news";
    }
 */
}
